//
//  ResetPassWordViewController_3.m
//  specialine
//
//  Created by 往返网络 on 17/2/8.
//  Copyright © 2017年 com. All rights reserved.
//

#import "ResetPassWordViewController_3.h"
#import "ResetPassView3.h"
#import "PublicMethod.h"
//定义新密码URL
#define URL_NEWPS [NSString stringWithFormat:@"%@/Appcustomernew/GetPassword/changePassword?type=get",BASE_URL]

@interface ResetPassWordViewController_3 ()<UITextFieldDelegate,ResetPassView3Delegate>

@property (nonatomic,strong)AFHTTPSessionManager* manger;

@end

@implementation ResetPassWordViewController_3

- (void)viewWillAppear:(BOOL)animated{
    

    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = YES;

}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self createUI];
    
    // Do any additional setup after loading the view.
}

- (void)viewWillDisappear:(BOOL)animated{

    [super viewWillDisappear:animated];
    
    self.navigationController.navigationBarHidden = NO;

}

- (void)createUI{

    ResetPassView3 * reset3 = [[ResetPassView3 alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H)];
    
    self.view.backgroundColor = [UIColor whiteColor];

    reset3.reset3delegate = self;

    [self.view addSubview:reset3];
    
    
    
}

#pragma mark -- -- - -协议代理方法 － － －－ － －－ － －
- (void)enterResetPassVC2BtnType:(ResetPassView3BtnType)resetPassType andFisrtstr:(NSString *)fisrtstr andsecondstr:(NSString *)secondstr{

    switch (resetPassType) {
            
        case ResetPassView3BtnClickReturn:
            
            NSLog(@"回到登陆页面");
            
            //回到登陆页面
           [self.navigationController dismissViewControllerAnimated:YES completion:nil];
            
            
            break;
            
            case ResetPassView3BtnClickNextBtn:
            
            
            [self changePSwithnewps:fisrtstr andsecondstr:secondstr];
            
            
            break;
            
        default:
            
            break;
            
    }



}

- (void)changePSwithnewps:(NSString *)firststr andsecondstr:(NSString *)secondstr{
    
    
    //开始动画
    UIImageView * im = [PublicMethod startAnimation];
    
    
    NSString * url = [NSString stringWithFormat:@"%@&mobile=%@&msgCode=%@&password=%@&configPassword=%@",URL_NEWPS,self.phonestring,self.msgcode,firststr,secondstr];
    
    NSLog(@"校验 验证码 url %@",url);
    
    [self.manger GET:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if (responseObject) {
            
            NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            
            NSInteger code = [dict[@"code"]integerValue];
            
            if (code == 0) {
                
                //获取验证码成功
                
                [im stopAnimating];
                
                NSLog(@"get code ok");
                
                ALERT(@"提示", dict[@"info"], self, @"确定", nil);
                
                //存储本地信息
                
                
                NSUserDefaults * userdefault = [NSUserDefaults standardUserDefaults];
                
                [userdefault setObject:@"islogin" forKey:USERDEFAULTS_ISLOGIN];
                
                [userdefault setObject:self.phonestring forKey:USERDEFAULTS_PHONE];
                
                // 存储tocken
                [userdefault setObject:dict[@"token"] forKey:USER_TOCKE];
                
                
                
                [self.navigationController dismissViewControllerAnimated:YES completion:nil];
                

                
                
            }else{
                
                [im stopAnimating];
                
                NSLog(@"get code no");
                
                ALERT(@"提示", dict[@"info"], self, @"确定", nil);
                
            }
            
            
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        [im stopAnimating];
        
        ALERT(@"提示", @"网络请求失败", self, @"确定", nil);
        
    }];
    
    
}

- (AFHTTPSessionManager*)manger{
    
    _manger = [AFHTTPSessionManager manager];
    
    _manger.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    _manger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", nil];
    
    return _manger;
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
